package com.imarec.web.persistence.dao;

import java.util.List;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import com.imarec.web.persistence.EntityManagerHolder;
import com.imarec.web.persistence.entities.ImageEntity;
import com.imarec.web.persistence.entities.ImageAttributeEntity;

public class ImageDAO 
{
	@SuppressWarnings("unchecked")
	public List<ImageAttributeEntity> getMatchingAttributes(int whiteCount, int tolerance, int redColor, int greenColor, int blueColor, int colorTolerance)
	{
		EntityManager em = EntityManagerHolder.getEntityManager();
		
		Query query = em.createQuery("SELECT a FROM ImageAttributeEntity a WHERE (ABS(a.whiteCount - ?1) < ?2) AND (ABS(a.redColor - ?3) < ?6) AND (ABS(a.greenColor - ?4) < ?6) AND (ABS(a.blueColor - ?5) < ?6)");
		query.setParameter(1, whiteCount);
		query.setParameter(2, tolerance);
		query.setParameter(3, redColor);
		query.setParameter(4, greenColor);
		query.setParameter(5, blueColor);
		query.setParameter(6, colorTolerance);
		
		return (List<ImageAttributeEntity>)query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<ImageAttributeEntity> getMatchingAttributesForColor(int redColor, int greenColor, int blueColor, int colorTolerance)
	{
		EntityManager em = EntityManagerHolder.getEntityManager();
		
		Query query = em.createQuery("SELECT a FROM ImageAttributeEntity a WHERE (ABS(a.redColor - ?3) < ?6) AND (ABS(a.greenColor - ?4) < ?6) AND (ABS(a.blueColor - ?5) < ?6)");
		query.setParameter(3, redColor);
		query.setParameter(4, greenColor);
		query.setParameter(5, blueColor);
		query.setParameter(6, colorTolerance);
		
		return (List<ImageAttributeEntity>)query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<ImageAttributeEntity> getMatchingAttributesForPixelCount(int whiteCount, int tolerance)
	{
		EntityManager em = EntityManagerHolder.getEntityManager();
		
		Query query = em.createQuery("SELECT a FROM ImageAttributeEntity a WHERE (ABS(a.whiteCount - ?1) < ?2)");
		query.setParameter(1, whiteCount);
		query.setParameter(2, tolerance);
		
		return (List<ImageAttributeEntity>)query.getResultList();
	}
	
	public void save(ImageEntity image, Set<ImageAttributeEntity> attributes)
	{
		EntityManager em = EntityManagerHolder.getEntityManager();
		
		em.getTransaction().begin();
		
		image.setImageAttributes(attributes);
		em.persist(image);
		
		em.getTransaction().commit();
	}
	
	public ImageEntity getImageByID(long id)
	{
		EntityManager em = EntityManagerHolder.getEntityManager();
		return em.find(ImageEntity.class, id);
	}
	
	@SuppressWarnings("unchecked")
	public List<ImageEntity> list(Integer firstResult, Integer resultCount)
	{
		EntityManager em = EntityManagerHolder.getEntityManager();
	
		Query query = em.createQuery("SELECT i FROM ImageEntity i ORDER BY i.id");
		query.setFirstResult(firstResult);
		query.setMaxResults(resultCount);
		
		return (List<ImageEntity>)query.getResultList();
	}
	
	public Integer getImagesCount()
	{
		EntityManager em = EntityManagerHolder.getEntityManager();
		
		Query query = em.createQuery("SELECT COUNT(i) FROM ImageEntity i");
		
		return ((Long)query.getSingleResult()).intValue();
	}
}
